/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tanks;
import com.sun.lwuit.*;
import com.sun.lwuit.animations.*;
import com.sun.lwuit.events.*;
import com.sun.lwuit.layouts.CoordinateLayout;
import com.sun.lwuit.layouts.BorderLayout;
import com.sun.lwuit.plaf.*;
import java.io.IOException;
import javax.microedition.midlet.*;
import com.sun.lwuit.Image;
import java.util.Timer;
import java.util.TimerTask;

/**
 * @author Александр
 */
public class Midlet extends MIDlet {
    Form helloForm;
    Form gameForm;
    Image LogoImg;
    Image GrassImg;
    Button StartGame;
    GameplayDraw draw;
    public void startApp() {
        Display.init(this);
        helloForm = new Form("Ribaplus tanks");
        try {
            LogoImg = Image.createImage("/images/TanksLogo.png");
            
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        StartGame = new Button("Start Game");
        StartGame.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent evt) {
                    //StartGame.setText("ButtonPressed!!11");
                    showAnotherForm();
            }
        });
        helloForm.setLayout(new CoordinateLayout(200,200));
        StartGame.setX(20);
        StartGame.setY(80);
        StartGame.setPreferredW(100);
        StartGame.setPreferredH(20);
        helloForm.addComponent(StartGame);
        helloForm.setBgImage(LogoImg);
        helloForm.show();
        Timer aTimer = new Timer();
        TimerTask ttask = new TimerTask() {
            public void run() {
                if(draw!=null){
                    draw.repaint();
                }
            }
        };
        aTimer.scheduleAtFixedRate(ttask, 0, 100);
    }
    int convertKey(int k){
        if(k>0){
            return k-48;
        }
        else{
            switch(k){
                case -1:
                    return 2;
                case -2:
                    return 8;
                case -3:
                    return 4;
                case -4:
                    return 6;
                case -5:
                    return 5;
                case -6:
                    return 1;
                case -7:
                    return 3;
                default:
                    return 0;
            }
        }
    }
    void Restart(){
        gameForm.removeComponent(draw);
        helloForm.show();
    }
    void showAnotherForm(){
        gameForm = new Form("Ribaplus tanks"){
            public void keyPressed(int keyCode) {
                Game.Instance().KeyList[convertKey(keyCode)] = true;
                if(Game.Instance().GameOver()){
                    Restart();
                }
            }
            public void keyReleased(int keyCode){
                Game.Instance().KeyList[convertKey(keyCode)] = false;
            }
        };
        gameForm.setLayout(new BorderLayout());
        System.out.println("Starting to draw field");
        draw = new GameplayDraw();
        //draw.invalidate();
        gameForm.addComponent(BorderLayout.CENTER,draw);
        System.out.println("Showing the form");
        gameForm.show();
    }
    public void pauseApp() {
    }
    
    public void destroyApp(boolean unconditional) {
    }
}
